from utils.load_data import load_in_file
from utils.preprocessing import *
import json
import numpy as np
import datetime
import pandas as pd

root_path = './data'
outputpath = f'{root_path}/widetable'

#def processing_infra():
def processing_infra(basic_path ,dates = ['06-17','06-18','06-19','06-20','06-21','06-24','06-27','06-28','06-29']):
    for i,date in  enumerate(dates):
        data_node = read_node_infra(basepath=f'{basic_path}/2025-{date}/metric-parquet/infra/',date=f'2025-{date}')
        data_pod = read_pod_infra(basepath  =f'{basic_path}/2025-{date}/metric-parquet/infra/',date=f'2025-{date}')
        data_tidb = read_tidb_infra(basepath  =f'{basic_path}/2025-{date}/metric-parquet/infra/',date=f'2025-{date}')
        data_tikv = read_tikv_infra(basepath  =f'{basic_path}/2025-{date}/metric-parquet/',date=f'2025-{date}')
        data_pd = read_pd_infra(basepath  =f'{basic_path}/2025-{date}/metric-parquet/',date=f'2025-{date}')

        data_node.to_csv(f'{outputpath}/2025-{date}/node_all.csv',index=False)
        data_pod.to_csv( f'{outputpath}/2025-{date}/pod_all.csv',index=False)
        data_tidb.to_csv(f'{outputpath}/2025-{date}/tidb_all.csv',index=False)
        data_tikv.to_csv(f'{outputpath}/2025-{date}/tikv_all.csv',index=False)
        data_pd.to_csv(  f'{outputpath}/2025-{date}/pd_all.csv',index=False)
        #if i == 0: 
        #    topo = pod2nodes(data_pod,data_node)
        #    print(topo)
        #else:
        #    topo = pod2nodes(data_pod,data_node,topo)

def processing_service(basic_path,dates = ['06-06','06-07','06-08','06-09','06-10','06-11','06-12','06-13']):
    for date in dates:
        data = servicelog(basepath=f'{basic_path}/2025-{date}/log-parquet',datetime=[f'2025-{date}_0{i}-00-00' if i< 10 else f'2025-{date}_{i}-00-00' for i in range(0,24)])
        data.preprocess()
        data.save()

def processing_trace(basic_path,dates = ['06-06','06-07','06-08','06-09','06-10','06-11','06-12','06-13']):
    for date in dates:
        data = tracelog(basepath=f'{basic_path}/2025-{date}/trace-parquet',datetime=[f'2025-{date}_0{i}-00-00' if i< 10 else f'2025-{date}_{i}-00-00' for i in range(0,24)])
        data.preprocess()
        data.save()

if __name__ == '__main__':
    basic_path = f'{root_path}/phasetwo'
    dates = ['06-17','06-18','06-19','06-20','06-21','06-24','06-27','06-28','06-29']
    
    processing_infra(basic_path,dates)

    basic_path = f'{root_path}/phaseone'
    dates = ['06-06','06-07','06-08','06-09','06-10','06-11','06-12','06-13','06-14']
    processing_infra(basic_path,dates)
